<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-row>
						<a-col :span="24">
							<a-form-item label="业务日期" v-bind="validateInfos.businessDate">
								<a-date-picker placeholder="请选择业务日期"  v-model:value="formData.businessDate" value-format="YYYY-MM"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销售组织" v-bind="validateInfos.salesOrganization">
								<a-input v-model:value="formData.salesOrganization" placeholder="请输入销售组织"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="部门" v-bind="validateInfos.department">
								<a-input v-model:value="formData.department" placeholder="请输入部门"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="客户" v-bind="validateInfos.client">
								<a-input v-model:value="formData.client" placeholder="请输入客户"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="品牌" v-bind="validateInfos.brand">
								<a-input v-model:value="formData.brand" placeholder="请输入品牌"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="物料分组" v-bind="validateInfos.materialGroup">
								<a-input v-model:value="formData.materialGroup" placeholder="请输入物料分组"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="旺店通编码" v-bind="validateInfos.wdtCode">
								<a-input v-model:value="formData.wdtCode" placeholder="请输入旺店通编码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="物料编码" v-bind="validateInfos.materialCode">
								<a-input v-model:value="formData.materialCode" placeholder="请输入物料编码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="物料名称" v-bind="validateInfos.materialName">
								<a-input v-model:value="formData.materialName" placeholder="请输入物料名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="货品收入" v-bind="validateInfos.goodsIncome">
								<a-input-number v-model:value="formData.goodsIncome" placeholder="请输入货品收入" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="货品成本" v-bind="validateInfos.costGoods">
								<a-input-number v-model:value="formData.costGoods" placeholder="请输入货品成本" style="width: 100%" />
							</a-form-item>
						</a-col>
            <a-col :span="24">
              <a-form-item label="货品数量" v-bind="validateInfos.num">
                <a-input-number v-model:value="formData.num" placeholder="请输入货品数量" style="width: 100%" />
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="货品重量" v-bind="validateInfos.num">
                <a-input-number v-model:value="formData.weight" placeholder="请输入货品重量" style="width: 100%" />
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="货品成本" v-bind="validateInfos.costGoods">
                <a-input-number v-model:value="formData.costGoods" placeholder="请输入货品成本" style="width: 100%" />
              </a-form-item>
            </a-col>
						<a-col :span="24">
							<a-form-item label="运输成本" v-bind="validateInfos.shippingCost">
								<a-input-number v-model:value="formData.shippingCost" placeholder="请输入运输成本" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="毛利额" v-bind="validateInfos.grossProfit">
								<a-input-number v-model:value="formData.grossProfit" placeholder="请输入毛利额" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="毛利率" v-bind="validateInfos.grossProfitMargin">
								<a-input v-model:value="formData.grossProfitMargin" placeholder="请输入毛利率"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="税金及附加" v-bind="validateInfos.taxesSurcharges">
								<a-input-number v-model:value="formData.taxesSurcharges" placeholder="请输入税金及附加" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销售费用-广告费" v-bind="validateInfos.adsFees">
								<a-input-number v-model:value="formData.adsFees" placeholder="请输入销售费用-广告费" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销售费用-资金账单扣费" v-bind="validateInfos.fundsBillDeduction">
								<a-input-number v-model:value="formData.fundsBillDeduction" placeholder="请输入销售费用-资金账单扣费" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销售费用-人员费用" v-bind="validateInfos.personnelExpenses">
								<a-input-number v-model:value="formData.personnelExpenses" placeholder="请输入销售费用-人员费用" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销售费用-其他" v-bind="validateInfos.otherExpenses">
								<a-input-number v-model:value="formData.otherExpenses" placeholder="请输入销售费用-其他" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="管理费用" v-bind="validateInfos.managementFees">
								<a-input-number v-model:value="formData.managementFees" placeholder="请输入管理费用" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="财务费用" v-bind="validateInfos.financialExpenses">
								<a-input-number v-model:value="formData.financialExpenses" placeholder="请输入财务费用" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="营业利润" v-bind="validateInfos.operatingProfit">
								<a-input-number v-model:value="formData.operatingProfit" placeholder="请输入营业利润" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="营业外收入" v-bind="validateInfos.nonOperatingIncome">
								<a-input-number v-model:value="formData.nonOperatingIncome" placeholder="请输入营业外收入" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="营业外支出" v-bind="validateInfos.nonOperatingExpenses">
								<a-input-number v-model:value="formData.nonOperatingExpenses" placeholder="请输入营业外支出" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="利润总额" v-bind="validateInfos.totalProfit">
								<a-input-number v-model:value="formData.totalProfit" placeholder="请输入利润总额" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="所得税费用" v-bind="validateInfos.incomeTaxExpense">
								<a-input-number v-model:value="formData.incomeTaxExpense" placeholder="请输入所得税费用" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="净利润" v-bind="validateInfos.netProfit">
								<a-input-number v-model:value="formData.netProfit" placeholder="请输入净利润" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="净利率" v-bind="validateInfos.netProfitMargin">
								<a-input-number v-model:value="formData.netProfitMargin" placeholder="请输入净利率" style="width: 100%" />
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../FFinancialProductStatements.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    businessDate: '',   
    salesOrganization: '',   
    department: '',   
    client: '',   
    brand: '',   
    materialGroup: '',   
    wdtCode: '',   
    materialCode: '',   
    materialName: '',   
    goodsIncome: undefined,
    costGoods: undefined,
    shippingCost: undefined,
    grossProfit: undefined,
    grossProfitMargin: '',   
    taxesSurcharges: undefined,
    adsFees: undefined,
    fundsBillDeduction: undefined,
    personnelExpenses: undefined,
    otherExpenses: undefined,
    managementFees: undefined,
    financialExpenses: undefined,
    operatingProfit: undefined,
    nonOperatingIncome: undefined,
    nonOperatingExpenses: undefined,
    totalProfit: undefined,
    incomeTaxExpense: undefined,
    netProfit: undefined,
    netProfitMargin: undefined,
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    wdtCode: [{ required: true, message: '请输入旺店通编码!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
